$(function () {
	
	
	    $('#dangerousGoodsDate').datetimepicker({
	        format: 'YYYY-MM-DD',
	        locale: moment.locale('zh-cn')
	    });
	    $('#effectDate').datetimepicker({
	        format: 'YYYY-MM-DD',
	        locale: moment.locale('zh-cn')
	    });
	    $('#inspectAnnually').datetimepicker({
	        format: 'YYYY-MM-DD',
	        locale: moment.locale('zh-cn')
	    });
		$('#billEffective_time').datetimepicker({
			format: 'YYYY-MM-DD',
			locale: moment.locale('zh-cn')
		});
	    
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/infomanager/list',
        datatype: "json",
        colModel: [			
			{ label: '车架子号', name: 'vehicleNumber', index: 'vehicle_number', width: 80 }, 			
			{ label: '车牌号', name: 'carNum', index: 'car_num', width: 80 }, 			
			{ label: '注册企业代码+自行定义代码', name: 'carInerCode', index: 'car_iner_code', width: 80 }, 			
			{ label: 'carId', name: 'carId', index: 'car_id', width: 50, key: true },
			{ label: '加油卡号', name: 'oilCardNum', index: 'oil_card_num', width: 80 }, 			
			{ label: '交强险单号', name: 'tci', index: 'tci', width: 80 }, 			
			{ label: '其他保单信息', name: 'otherInsurance_policy', index: 'other_insurance_policy', width: 80 },
			{ label: '保单生效时间', name: 'billEffective_time', index: 'bill_effective_time', width: 80 },
			{ label: '是否危险品', name: 'ifDangerousGoods', index: 'if_dangerous_goods', width: 80 }, 			
			{ label: '危险品运营证时间', name: 'dangerousGoodsDate', index: 'dangerous_goods_date', width: 80 }, 			
			{ label: '有效期', name: 'effectDate', index: 'effect_date', width: 80 }, 			
			{ label: '（1自有、2挂靠、3临时）', name: 'vehicleType', index: 'Vehicle_type', width: 80 }, 			
			{ label: '车辆年审时间', name: 'inspectAnnually', index: 'inspect_annually', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			carNum: null
		},
		showList: true,
		title: null,
		infoManager: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.infoManager = {};
		},
		update: function (event) {
			var carId = getSelectedRow();
			if(carId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(carId)
		},
		saveOrUpdate: function (event) {
			var url = vm.infoManager.carId == null ? "sys/infomanager/save" : "sys/infomanager/update";
            vm.infoManager.inspectAnnually =  $('#inspectAnnually').val();
            vm.infoManager.dangerousGoodsDate =  $('#dangerousGoodsDate').val();
            vm.infoManager.effectDate =  $('#effectDate').val();
            vm.infoManager.billEffective_time =  $('#billEffective_time').val();
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.infoManager),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var carIds = getSelectedRows();
			if(carIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/infomanager/delete",
                    contentType: "application/json",
				    data: JSON.stringify(carIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(carId){
			$.get(baseURL + "sys/infomanager/info/"+carId, function(r){
                vm.infoManager = r.infoManager;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'carNum': vm.q.carNum},
                page:page
            }).trigger("reloadGrid");
		}
	}
});